#!/usr/bin/env python
# vi: et sw=2

import os.path
import sys
from xml.dom.minidom import parse, parseString, getDOMImplementation

APPLET_PATH = '/usr/share/gnome-pulse-applet/'
HOME_PATH   = '~/.gnome-pulse-applet/'
STREAM_XML  = 'icons.xml'

class StreamIcons:
    
    def __init__(self, path = HOME_PATH, filename = STREAM_XML ):
        self.stream_icons = []
        self.path = os.path.expanduser(path)
        self.file = os.path.expanduser(path + filename)       
        self.checkFile()

        return
                            
    def checkFile(self):
			
        if not os.path.isdir(self.path):
            os.mkdir(self.path)        

        if os.path.isfile(self.file):
            return True
        else:    
            ## If file doesn't exist but it is writable
            ## Copy original file from share/					
            file = open(APPLET_PATH + 'icons.xml','r')
            data = file.read()
            file.close()
            
            file = open(self.file,'w')
            file.write(data)
            file.close()            
            return True

        return False

    def loadStreamIcons(self, distro = ""):	
        self.stream_icons = []
        if os.path.isfile(self.file):
            data = parse(self.file)
            icons = data.getElementsByTagName("stream")
            for icon in icons:
                stream_distro = icon.getAttribute("distro")
                if stream_distro == distro or stream_distro == "" or distro == "":
                    stream_name = icon.getAttribute("name")
                    stream_icon = icon.getAttribute("icon")
                    stream = StreamIcon(stream_name,stream_distro,stream_icon)
                    self.stream_icons.append(stream)
        return
            
    def setStreamIcon(self,stream_number,column,value):
								
        if stream_number == len(self.stream_icons):
            stream = StreamIcon("","","")
            self.stream_icons.append(stream)        
            
        if column == 1:
            self.stream_icons[stream_number].setName(value)
        elif column == 2:
            self.stream_icons[stream_number].setDistro(value)
        else:
            self.stream_icons[stream_number].setPath(value)
            
        return

    def removeStreamIcon(self,stream_number):
        self.stream_icons.pop(stream_number)
        return
    
    def getStreamIcon(self,stream_name):
        if self.stream_icons.has_key(stream_name):
            return self.stream_icons[stream_name] 
        
        return None

    
    def searchStreamIcon(self,stream_name):
        import re
        for icon in self.stream_icons:
            if re.search(icon.name, stream_name, re.I):
                return icon.path
        return None


    def listAllStreamIcons(self):
        return self.stream_icons
    

    def saveToXML(self):
        impl = getDOMImplementation() 
        newdoc = impl.createDocument(None, "streams", None)
        top_element = newdoc.documentElement
        for stream_icon in self.stream_icons:
            tag = newdoc.createElement("stream")
            tag.setAttribute("name",stream_icon.name)
            if stream_icon.distro != "":            
                tag.setAttribute("distro",stream_icon.distro)
            tag.setAttribute("icon",stream_icon.path)
            top_element.appendChild(tag)         
        
        xml = newdoc.toxml("utf-8")
				
        ## If file is found write to xml file
        if os.path.isfile(self.file) and os.access(self.file, os.W_OK):				
            fp = open(self.file,"w")
            newdoc.writexml(fp, "    ", "", "\n", "UTF-8")
            fp.close()
        return

class StreamIcon:
    def __init__(self,name,distro,path):
        self.name = name
        self.distro = distro
        self.path = path
        return
    
    ## Getters 
    def getName(self):
        return self.name
    
    def getDistro(self):
        return self.distro
    
    def getPath(self):
        return self.path
    
    ## Setters

    def setName(self,name):
        self.name = name
        return
    
    def setDistro(self,distro):
        self.distro = distro
        return    
    def setPath(self,path):
        self.path = path
        return        
